package com.hzit.mmcb.core.controller.api;


import com.hzit.common.exception.Assert;
import com.hzit.common.result.ResponseEnum;
import com.hzit.mmb.base.utils.JwtUtils;
import com.hzit.mmcb.core.pojo.dto.UserInfoDTO;
import com.hzit.mmcb.core.pojo.entity.UserInfo;
import com.hzit.mmcb.core.pojo.entity.UserLoginRecord;
import com.hzit.mmcb.core.pojo.vo.RegisterVO;
import com.hzit.mmcb.core.pojo.vo.UserIndexVO;
import com.hzit.mmcb.core.pojo.vo.UserLoginVO;
import com.hzit.mmcb.core.service.UserInfoService;
import com.hzit.common.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author ljh
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/api/core/userInfo")
@Api(tags = "用户信息")
public class ApiUserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("/list")
    @ApiOperation("查询用户信息列表")
    public R list(){
        return R.ok().data("list",userInfoService.list());
    }


    /**
     * 注册用户
     * @param registerVO 用户注册信息
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public R register(@RequestBody @ApiParam(value = "注册信息",required = true) RegisterVO registerVO){
        userInfoService.register(registerVO);
        return R.ok().message("注册成功");
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public R login(@RequestBody @ApiParam(value = "用户登录信息",required = true) UserLoginVO userLoginVO,
                   HttpServletRequest request){
        String ip = request.getRemoteAddr();
        UserInfoDTO userInfoDTO =userInfoService.login(userLoginVO,ip);
        return R.ok().data("userInfo",userInfoDTO);
    }


    @GetMapping("checkToken")
    @ApiOperation("token校验")
    public R checkToken(HttpServletRequest request){
        String token = request.getHeader("token");
        boolean b = JwtUtils.checkToken(token);
        Assert.isTrue(b,ResponseEnum.LOGIN_AUTH_ERROR);
        return R.ok();


    }

    @GetMapping("/checkMobile/{mobile}")
    @ApiOperation("验证手机号码是否已经注册过了")
    public R checkMobile(@PathVariable @ApiParam("手机号码") String  mobile){
        boolean b=userInfoService.checkMobile(mobile);
        return R.ok().data("isExist",b);
    }

    @GetMapping("/auth/getIndexUserInfo")
    public R getIndexUserInfo(@RequestHeader("token")String token){
        Long userId = JwtUtils.getUserId(token);
        UserIndexVO userIndexVO=userInfoService.getIndexUserInfo(userId);
        return R.ok().data("userIndexVO",userIndexVO);
    }




}

